import { baseRequestClient } from '#/api/request'
import { requestClient } from '#/api/request'
import  {requestClient_upload} from '#/api/upload'

// 查询人员基本信息列表
// 查询人员参数
interface UserItem {
  userName?: string;
  status?: number;
}
// 查询人员返回值
interface ListUserResult {
  rows: any;
  total: number;
}

//添加人员参数
export interface AddUserParams extends Record<string, any> {
  userName: string;
  status: number;
  desc: string;
  realName: string;
  avatar: string;
  role: string;
  remark: string;
  email: string;
  phoneNumber: string;
  sex: number;
  delFlag: number;
}

//修改人员参数
export interface EditUserParams extends Record<string,any> {
  userName: string;
  status: number;
  desc: string;
  realName: string;
  avatar: string;
  role: string;
  remark: string;
  email: string;
  phoneNumber: string;
  sex: number;
  delFlag: number;
}

//获取用户分页数据
export const listUser = async (params: {
  pageNum: number;
  pageSize: number;
  userName?: string;
  status?: string;
}) => {
  const response = await requestClient.get<ListUserResult>('/manage/user/list', { params });
  const data = response.rows; // 提取 rows 字段
  console.log("data:",data);
  let filteredData: UserItem[] = data;

  const total = response.total;
  const result = filteredData;

  //处理数据
  const mappedResult = result.map((row: any) => {
    return {
      ...row,
      statusFormatted: row.status === 0 ? '正常' : '冻结',
      sexFormatted: row.sex === 0 ? '男' : '女',
      roleFormatted: row.role === 'super' ? '管理员' : row.role === 'tech' ? '技术员' : row.role === 'opex' ? '运维员' : row.role ===4 ? '游客' : '未知角色',
    };
  });
  console.log("mappedResult:",mappedResult);
  

  return {
    items: mappedResult,
    total: total,
  };
};

// 查询人员基本信息详细
export async function getUser(id:any) {
  return baseRequestClient.get(`/manage/user/${id}`)
}

// 新增人员基本信息
export async function addUser(data:AddUserParams) {
  await requestClient.post('/manage/user', data);
}

// 修改人员基本信息
export async function updateUser(data:EditUserParams) {
  requestClient.put('/manage/user', data);
}

// 删除人员基本信息
export async function delUser(id:number) {
  requestClient.delete(`/manage/user/${id}`)
}

// 用户密码重置
export async function updateUserPwd(oldPassword: string, newPassword: string) {
  const data = {
    oldPassword,
    newPassword
  }
  return requestClient.put('/system/user/profile/updatePwd', data)
}

// 查询用户个人信息
export async function getUserInfo() {
  return requestClient.get('/manage/user')
}

// 修改用户个人信息
export async function updateUserProfile(data: any) {
  return requestClient.put('/system/user/profile', data)
}

